package client.comm.baoding.ui


import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.main.ZhihuanGoodAdapter
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.api.bean.SuccessEvent
import client.comm.baoding.api.bean.TradeIndex
import client.comm.baoding.databinding.ActivityZhihuanBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.ZhihuanViewModel
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import com.blankj.utilcode.util.LogUtils
import org.greenrobot.eventbus.EventBus

class ZhihuanListActivity : BaseMiddleActivity<ZhihuanViewModel, ActivityZhihuanBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zhihuan
    }

    val adapter: ZhihuanGoodAdapter by lazy {
        ZhihuanGoodAdapter(this@ZhihuanListActivity, mImgLoader).apply {
            zhihuan = {

                MsgTitleDialog(this@ZhihuanListActivity).apply {
                    title = "是否确认置换？"
                    ok = {
                        //订单商品goods_id goods_bean.goods_id
                        LogUtils.i("订单商品goods_id=" + goods_bean?.order_id + "===要置换的goods_id=" + it.goods_id)

                        viewModel.zhihuan("${goods_bean?.order_id}", it.goods_id)
                    }
                    okTxt = "确认"
                    cancleTxt = "取消"
                }.show()
            }

        }
    }

    override fun initViewModel(): ZhihuanViewModel = createViewModel()

    var goods_bean: GoodsOrder.Goods? = null

    override fun initView() {
        goods_bean = intent.getParcelableExtra<GoodsOrder.Goods>("goods_bean")

        binding?.apply {
            event = this@ZhihuanListActivity
            recyclerView.layoutManager = LinearLayoutManager(this@ZhihuanListActivity)
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(SpaceItemDecoration(10))
            emptyLayout.setOnClickListener { viewModel.getListCall() }
        }

        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageTrade.value = 0
            viewModel.getListCall()
        }

        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isLoading.value!!) {
                viewModel.getListCall()
            }
        }
        viewModel.isLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        binding.emptyLayout.setOnClickListener {
            viewModel.pageTrade.value = 0
            viewModel.getListCall()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getListCall()
    }

    override fun observerUI() {
        viewModel.tradeCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list = it.data?.goods_list as MutableList<TradeIndex.Goods?>?
                    if (viewModel.pageTrade.value == 1) {
                        adapter.submitList(list)
                    } else {
                        adapter.appendList(list)
                    }

                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0) {
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        }
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }
            }
        })

        viewModel.zhihuanRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("置换成功")
                    EventBus.getDefault().post("zhihuan_success")

                    finish()
                }
            }
        })
    }


}